package com.example.holes;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Timer {
	private int MAX_TIME = 60 * Global.UPS; // time in SECONDS
	private int current = MAX_TIME;
	private boolean timeUp = false;
	private final float leftStart, topStart;
	private float left, top;
	private final float leftInc;
	private final float topDec1, topDec2, topDec3, topDec4;
	private final float topInc1, topInc2, topInc3, topInc4;
	private final float seven8ths, six8ths, five8ths,
						four8ths, three8ths, two8ths, one8th;
	Bitmap image;

	public Timer(Bitmap image){
		this.image = image;
		leftStart = 220 * Global.SCALE_RATIO_W;
		topStart = 109 * Global.SCALE_RATIO_H;
		left = leftStart;
		top = topStart;
		// moon X position increment
		leftInc = (float)400 * Global.SCALE_RATIO_W / MAX_TIME;
		// moon Y position decrement (for RISING)
		topDec1 = (float)66*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		topDec2 = (float)22*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		topDec3 = (float)8*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		topDec4 = (float)4*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		// moon Y position increment (for SETTING)
		topInc1 = (float)4*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		topInc2 = (float)8*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		topInc3 = (float)25*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		topInc4 = (float)75*Global.SCALE_RATIO_H/((float)MAX_TIME/8);
		// divisions of animation
		seven8ths = (float)(MAX_TIME / 8) * 7;
		six8ths = (float)(MAX_TIME / 8) * 6;
		five8ths = (float)(MAX_TIME / 8) * 5;
		four8ths = (float)(MAX_TIME / 8) * 4;
		three8ths = (float)(MAX_TIME / 8) * 3;
		two8ths = (float)(MAX_TIME / 8) * 2;
		one8th = (float)(MAX_TIME / 8);
	}
	
	// =========================
	//        v  UPDATE  v
	// =========================
	// subtracts milliseconds that have gone by in one tick
	public void tick(){
		current--;
		if (0 <= current){
			left += leftInc;
			if (current > seven8ths)
				top -= topDec1;
			else if (current > six8ths)
				top -= topDec2;
			else if (current > five8ths)
				top -= topDec3;
			else if (current > four8ths)
				top -= topDec4;
			else if (current > three8ths)
				top += topInc1;
			else if (current > two8ths)
				top += topInc2;
			else if (current > one8th)
				top += topInc3;
			else
				top += topInc4;
		}
		else {
			current = 0;
			timeUp = true;
		}
	}
	// =========================
	//        ^  UPDATE  ^
	// =========================
	
	// =========================
	//         v  DRAW  v
	// =========================
	public void Draw(Canvas c){
		c.drawBitmap(image, left, top, null);
	}
	// =========================
	//         ^  DRAW  ^
	// =========================
	
	
	public int getTicksLeft(){ return current; }
	public boolean getTimeUp(){ return timeUp; }
	public void reset(){
		current = MAX_TIME;
		timeUp = false;
		left = leftStart;
		top = topStart;
	}
}
